<?php
class CartillasController extends AppController {

	var $name = 'Cartillas';

	function index() {
		$this->Cartilla->recursive = 0;
		$this->set('cartillas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid cartilla', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('cartilla', $this->Cartilla->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Cartilla->create();
			if ($this->Cartilla->save($this->data)) {
				$this->Session->setFlash(__('The cartilla has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cartilla could not be saved. Please, try again.', true));
			}
		}
		$grados = $this->Cartilla->Grado->find('list');
		$this->set(compact('grados'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cartilla', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Cartilla->save($this->data)) {
				$this->Session->setFlash(__('The cartilla has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cartilla could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Cartilla->read(null, $id);
		}
		$grados = $this->Cartilla->Grado->find('list');
		$this->set(compact('grados'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cartilla', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Cartilla->delete($id)) {
			$this->Session->setFlash(__('Cartilla deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Cartilla was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
